// EditorLibrary.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "LitEditorInstance.h"
#include <qmainwindow.h>

namespace Lit
{
	namespace Editor
	{
		void Instance::ogreInitialise(){
			Lit::Root::Instance().initialise( 
				mRenderWidget->ogreRenderWindow(),
				mRenderWidget->ogreCamera(),
				mRenderWidget->ogreSceneManager(),
				mComponents,
				mName+".log",
				true
			);
			mLogger->initialise( *Lit::Root::Instance().log() );
			mLogger->logMessage( "Instance Initialised !" );
		}
		void Instance::ogreShutdown(){			
			mLogger->logMessage( "Instance Deinitialised !" );
			Lit::Root::Instance().shutdown();
			mLogger->deInitialise();
		}
		Instance::Instance( const std::string &name, QMainWindow * main_win, QDockWidget * log_dock )
			: Initialiser( "Instance '"+name+"'" )
			, mName( name )
			, mMainWindow( main_win )
			, mLogger( new Logger( log_dock ) )
			, mRenderWidget( NULL )
		{}
		Instance::~Instance()
		{
		}
		
		const std::vector<std::string> & Instance::getCommandArguments() const{ return mArgs; }
		std::vector<std::string> & Instance::getCommandArguments(){ return mArgs; }

		void Instance::setCommandArguments( const QStringList &args_ )
		{
			QStringList args(args_);
			if( !args.isEmpty() )
			{
				if( FilePath( FROM_QT(args[0]) ).dirName() == FilePath( FileSystem::applicationPath() ).str() )
					args.erase( args.begin() );
			}
			mArgs.clear();
			for( auto i=args.begin(); i!=args.end(); i++ )
			{
				mArgs.push_back( FROM_QT((*i)) );
			}
		}
		Ogre::SceneManager * Instance::getSceneManager(){ return mRenderWidget->ogreSceneManager();}
		const Ogre::SceneManager * Instance::getSceneManager() const{ return mRenderWidget->ogreSceneManager();}

		Ogre::Camera* Instance::getSceneCamera(){ return mRenderWidget->ogreCamera();}
		const Ogre::Camera* Instance::getSceneCamera() const{ return mRenderWidget->ogreCamera();}

		const QMainWindow * Instance::getMainWindow() const{ return mMainWindow;}
		QMainWindow * Instance::getMainWindow(){ return mMainWindow;}

		const RenderWidget* Instance::getRenderWidget() const{ return mRenderWidget;}
		RenderWidget * Instance::getRenderWidget(){ return mRenderWidget;}

		const Logger* Instance::getLogger() const{ return mLogger;}
		Logger* Instance::getLogger(){ return mLogger;}
		
		const std::string &Instance::getName() const{ return mName; }
		
		void Instance::addRenderListener( RenderWidgetListener * lst ){ if( mRenderWidget ) mRenderWidget->addListener(lst);}
		void Instance::removeRenderListener( RenderWidgetListener * lst ){ if( mRenderWidget ) mRenderWidget->removeListener(lst);}
		bool Instance::hasRenderListener( RenderWidgetListener * lst ) const{ if( mRenderWidget )return mRenderWidget->hasListener(lst); return false;}
		
		void Instance::initialise( CoreComponentList components, RenderWidget * widget, RenderWidgetListener * lst ){
			throwAlreadyInit( );
			mComponents = components;
			if( !widget ) throw InvalidParameter( "RenderWidget", "null pointer", EXC_LOC );
			if( widget ) mRenderWidget = widget;
			else
			{
				mRenderWidget = new RenderWidget( mMainWindow );
				mMainWindow->setCentralWidget( mRenderWidget );
			}
			if( lst )
				mRenderWidget->addListener(lst);
			connect( mRenderWidget, SIGNAL(ogreInitialise()), this, SLOT(ogreInitialise()) );
			connect( mRenderWidget, SIGNAL(ogreShutdown()), this, SLOT(ogreShutdown()) );
			setInitialised( true );
		}
		void Instance::deInitialise( RenderWidgetListener * lst ){
			throwNotInit( );
			if( mRenderWidget ) delete mRenderWidget, mRenderWidget = NULL;
			setInitialised( false );
		}
	};
};